/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#include "MkRtspResponse.h"
#include "MkRtspUtil.h"
#include "MkUtil/MkUtil.h"
#include "MkUtil/MkLog.h"

CMkRtspResponse::CMkRtspResponse()
    :m_Version("1.0")
{

}

Uint32 CMkRtspResponse::OnPacketHeaderLine(OUT MkString& strHeader)
{
    MkString StatusCode = CMkRtspUtil::GetInstance().GetStatusCodeReason(m_StatusCode);
    if (StatusCode.empty()) {
        MkErrorLog("set a invalid status code\n");
        return InvalidParams;
    }
    strHeader = "RTSP/" + m_Version + " " + std::to_string(m_StatusCode) + " " + StatusCode;
    return NoneError;
}

Uint32 CMkRtspResponse::OnParseHeaderLine(const MkString& strHeader)
{
    MkVector<MkString> VecStr;
    Uint32 ErrorCode = CMkUtil::StringSplit(strHeader, " ", VecStr);
    MkCheckErrorLog(ErrorCode, "split header:%s error\n", strHeader.c_str());
    if (VecStr.size() < 3) {
        MkErrorLog("header:%s is invalid\n", strHeader.c_str());
        return InvalidParams;
    }
    size_t Pos = VecStr[0].find("RTSP/");
    if (MkString::npos == Pos) {
        MkErrorLog("rtsp response version(%s) invalid\n", VecStr[0].c_str());
        return InvalidParams;
    }
    m_StatusCode = static_cast<MkRtspStatusCode>(std::stoi(VecStr[1]));
    m_ReasonPhrase = VecStr[2];
    return NoneError;
}

CMkRtspResponse::~CMkRtspResponse()
{

}